package com.wanjia.estate.pojo.httpservice;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.Period;
import java.util.List;
import java.util.Objects;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Jmhxx {

    @Data
    public static class Request implements Serializable {
        private List<RequestData> data;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class RequestData implements Serializable {
        /**
         * 身份证
         */
        private String sfzh;
    }

    @Data
    public static class Response implements Serializable {
        private Integer code;
        private String msg;
        private ResponseData data;

        public boolean isSuccessful() {
            return Objects.equals(code, 200) && Objects.equals(msg, "done");
        }
    }

    @Data
    public static class ResponseData implements Serializable {
        private String status;
        @JSONField(name = "hcy_xx")
        private HcyXx hcyXx;

        public boolean isSuccessful() {
            return Objects.equals(status, "succeed");
        }
    }

    @Data
    public static class HcyXx implements Serializable {
        private String exception;
        private String total;
        private List<Result> results;
    }

    @Data
    public static class Result implements Serializable {
        @JSONField(name = "CSRQ")
        private String csrq;
        @JSONField(name = "GMSFHM")
        private String gmsfhm;
        @JSONField(name = "HH")
        private String hh;
        @JSONField(name = "XB")
        private String xb;
        @JSONField(name = "XB_ZW")
        private String xbZw;
        @JSONField(name = "XM")
        private String xm;
        @JSONField(name = "YHZGX")
        private String yhzgx;
        @JSONField(name = "YHZGX_ZW")
        private String yhzgxZw;

        /**
         * 根据出生日期计算当前年龄
         *
         * @return 当前年龄
         */
        public Integer calculateYear() {
            try {
                //获取户口部的出生日期
                String yearAge = csrq.substring(0, 4);
                String monthAge = csrq.substring(4, 6);
                String dateAge = csrq.substring(6, 8);
                String strBirthday = yearAge + "-" + monthAge + "-" + dateAge;
                LocalDate birthday = LocalDate.parse(strBirthday);
                //获取现在日期
                LocalDate now = LocalDate.now();
                Period period = Period.between(birthday, now);
                //得到现在年龄
                return period.getYears();
            } catch (Exception e) {
                throw new RuntimeException("计算年龄异常", e);
            }
        }
    }
}
